

const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerControl extends cc.Component {
    //音频
    @property(cc.AudioClip)
    MoveAudio: cc.AudioClip = null;
    @property(cc.AudioClip)
    FireAudio: cc.AudioClip = null;
    @property(cc.AudioClip)
    BoomAudio: cc.AudioClip = null;
    @property(cc.Node)
    BgNode:cc.Node = null;
    //子弹预制体
    @property(cc.Prefab)
    BulletPre: cc.Prefab = null;
    //获取刚体
    PlayerBody: cc.RigidBody;
    //水平，垂直轴
    vertical: number = 0;
    horizontal: number = 0;
    //开火控制
    isFire: boolean = true;
    //坦克速度
    TankSpeed: number = 25;

    onLoad() {
        //开启物理检测
        cc.director.getPhysicsManager().enabled = true;
    }

    start() {
        this.input();
        this.PlayerBody = this.getComponent(cc.RigidBody);
    }

    onBeginContact(concat,slef,other){
        // console.debug(other.tag)
    }

    died () {
        cc.audioEngine.playEffect(this.BoomAudio,false)
        this.node.destroy();
        let MoveAudioID = cc.audioEngine.playMusic(this.MoveAudio, true);
        cc.audioEngine.stop(MoveAudioID);
        if (this.BgNode.active == false) {
            this.BgNode.active = true;
        }
    }

    fire() {
        //实例化子弹
        let PlayerBullet = cc.instantiate(this.BulletPre);
        //设置父节点
        PlayerBullet.setParent(cc.director.getScene());
        //角度设置与当前节点相同
        PlayerBullet.angle = this.node.angle;
        //获得当前节点的世界坐标系
        let BulletWorldPor = this.node.convertToWorldSpaceAR(cc.v2(0, 0));
        //根据当前角度设置子弹初始位置
        if (PlayerBullet.angle == 0) {
            PlayerBullet.x = BulletWorldPor.x;
            PlayerBullet.y = BulletWorldPor.y + 15;
        } else if (PlayerBullet.angle == 180) {
            PlayerBullet.x = BulletWorldPor.x;
            PlayerBullet.y = BulletWorldPor.y - 15;
        } else if (PlayerBullet.angle == 90) {
            PlayerBullet.x = BulletWorldPor.x - 15;
            PlayerBullet.y = BulletWorldPor.y;
        } else if (PlayerBullet.angle == 270) {
            PlayerBullet.x = BulletWorldPor.x + 15;
            PlayerBullet.y = BulletWorldPor.y;
        }
        // cc.audioEngine.play(this.FireAudio, false, 2);
        cc.audioEngine.playEffect(this.FireAudio, false);
    }

    input() {
        //键盘按下
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, (event) => {
            //水平
            if (event.keyCode == cc.macro.KEY.a) {
                this.horizontal = -1;
            } else if (event.keyCode == cc.macro.KEY.d) {
                this.horizontal = 1;
            }
            //垂直
            if (event.keyCode == cc.macro.KEY.s) {
                this.vertical = -1;
            } else if (event.keyCode == cc.macro.KEY.w) {
                this.vertical = 1;
            }
            //开火
            if (event.keyCode == cc.macro.KEY.space) {
                //判断是否已开火
                if (this.isFire) {
                    this.fire();
                    this.isFire = false;
                }
            }

        })
        //键盘抬起
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, (event) => {
            //水平
            if (event.keyCode == cc.macro.KEY.a && this.horizontal == -1) {
                this.horizontal = 0;
            } else if (event.keyCode == cc.macro.KEY.d && this.horizontal == 1) {
                this.horizontal = 0;
            }
            //垂直
            if (event.keyCode == cc.macro.KEY.s && this.vertical == -1) {
                this.vertical = 0;
            } else if (event.keyCode == cc.macro.KEY.w && this.vertical == 1) {
                this.vertical = 0;
            }
            //开火
            if (event.keyCode == cc.macro.KEY.space) {
                this.isFire = true;
            }
        })
    }

    update(dt) {
        //水平移动
        if (this.horizontal != 0) {
            this.PlayerBody.linearVelocity = cc.v2(this.horizontal * this.TankSpeed, 0);
            this.node.angle = this.horizontal > 0 ? 270 : 90;
            //播放音效
            if (cc.audioEngine.isMusicPlaying() == false) {
                cc.audioEngine.playMusic(this.MoveAudio, true);
            }
            //垂直移动
        } else if (this.vertical != 0) {
            this.PlayerBody.linearVelocity = cc.v2(0, this.vertical * this.TankSpeed);
            this.node.angle = this.vertical > 0 ? 0 : 180;
            //播放音效
            if (cc.audioEngine.isMusicPlaying() == false) {
                cc.audioEngine.playMusic(this.MoveAudio, true);
            }
            //停止移动
        } else {
            this.PlayerBody.linearVelocity = cc.v2(0, 0);
            //停止播放
            if (cc.audioEngine.isMusicPlaying() == true) {
                let MoveAudioID = cc.audioEngine.playMusic(this.MoveAudio, true);
                cc.audioEngine.stop(MoveAudioID);
            }
        }


    }
}
